import urllib


class ProxyScraperApiMiddleware(object):
    def _set_request(self, request):
        if "http://api.scraperapi.com" in request.url:
            return request.url
        else:
            request.raw_url = request.url
            request.url = self.scraperapi_url2(request.url)

    @staticmethod
    def scraperapi_url2(url):
        encoded_url = urllib.parse.urlencode({"url": url})
        scarper_url = "http://api.scraperapi.com?key=21e40228e971e1dc070d20a2f456e82a&"+encoded_url+"&keep_headers=true"
        return scarper_url
